import Vue from "vue";
import VueRouter from "vue-router";
import store from "@/store";
Vue.use(VueRouter);

// 主应用界面的 router-view 一级渲染组件
export let appContextRouteConfig = {
  path: "/appContext",
  name: "appContext",
  component: () => import("@/pages/AppContext.vue"),
  children: [],

};

const routes = [
  {
    path: "/logon",
    name: "Logon",
    component: () => import("@/pages/Logon.vue"),
  },
  {
    path: "/reg",
    name: "Reg",
    component: () => import("@/pages/Reg.vue"),
  },
  // appContextRouteConfig,
  // {
  //   path: "/appContext",
  //   name: "appContext",
  //   component: () => import("@/pages/AppContext.vue"),
  //   children: [
  //     {
  //       component: () => import("@/views/DashBoard"),
  //       path: "/dashBoard",
  //       name: "DashBoard",
  //     },
  //     {
  //       component: () => import("@/views/LeaveApply"),
  //       path: "/leaveApply",
  //       name: "LeaveApply",
  //     },
  //     // {
  //     //   component: () => import("@/views/LeaveAudit"),
  //     //   path: "/leaveAudit",
  //     //   name: "LeaveAudit",
  //     // },
  //   ],
  // },
];

const router = new VueRouter({
  routes,
});

//判定用户是否登录

//判定用户访问的地址是否是白名单路由/黑名单路由

//导航守卫 拦截器 过滤器 interceptor filter aop guard 钩子函数
//谁去 去哪里
let whiteRoutes  = ["/logon","/reg"];
router.beforeEach((to, from, next) => {
  console.log("用户是否登录： ", store.state.isAuth);

  if (store.state.isAuth) {
    if (to.path === "/logon") {
      next("/dashboard");
    } else {
      next();
    }
  } else {
    if (whiteRoutes.some(x=>x === to.path)) {
      next();
    } else {
      next("/logon");
    }
  }

});
export default router;
